﻿using AutoMapper;
using KayakMes.Check.Application.Input;
using KayakMes.Check.Application.OUT;
using KayakMes.Check.Domain;
using KayakMes.Common;
using KayakMes.Common.Result;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Yitter.IdGenerator;

namespace KayakMes.Check.Application.Quotation
{
    public class Quotationservice : IQuotationService
    {
        private readonly BaseReposiotry<QuotationModel> _quotationReposiotry;

        private readonly IMapper _mapper;
        public Quotationservice(BaseReposiotry<QuotationModel> quotationReposiotry, IMapper mapper)
        {
            _quotationReposiotry = quotationReposiotry;
            _mapper = mapper;
        }
        /// <summary>
        /// 添加报价
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public ApiResult<int> AddQuotation(AddQuotationinput input)
        {
            ApiResult<int> apiResult = new ApiResult<int>();
             var model = _mapper.Map<QuotationModel>(input);
            model.CreateTime = DateTime.Now;
            model.IsDeleted = false;
            model.Id = YitIdHelper.NextId();
            var result = _quotationReposiotry.Add(model);
            if (result > 0)
            {
                apiResult.Code = ApiEnums.Success;
                apiResult.Message = "添加成功";
                apiResult.Data = result;
            }
            else
            {
                apiResult.Code = ApiEnums.Fail;
                apiResult.Message = "添加失败";
                apiResult.Data = 0;
            }
            return apiResult;
        }
        /// <summary>
        /// 获取所有报价
        /// </summary>
        /// <param name="input">查询参数</param>
        /// <returns>分页结果</returns>
        public ApiPaging<QuotationOut> GetAllQuotation(QuotationInput input)
        {
            try
            {
                // 获取基础查询
                var query = _quotationReposiotry.GetAll().Where(x => !x.IsDeleted);

                // 条件筛选
                // 1. 客户编号条件
                if (!string.IsNullOrEmpty(input.CustomerCode))
                {
                    query = query.Where(x => x.CustomerCode.Contains(input.CustomerCode));
                }

                // 2. 客户名称条件
                if (!string.IsNullOrEmpty(input.CustomerName))
                {
                    query = query.Where(x => x.CustomerName.Contains(input.CustomerName));
                }

                // 获取总记录数
                var total = query.Count();

                // 分页查询
                var list = query.Skip((input.PageIndex - 1) * input.PageSize)
                              .Take(input.PageSize)
                              .OrderByDescending(x => x.CreateTime)  // 按创建时间倒序排序
                              .ToList();

                // 使用AutoMapper转换为输出对象
                var result = _mapper.Map<List<QuotationOut>>(list);

                // 返回分页结果
                return new ApiPaging<QuotationOut>
                {
                    Code = ApiEnums.Success,
                    Message = "查询成功",
                    Total = total,
                    Items = result,
                    PageIndex = input.PageIndex,
                    PageSize = input.PageSize
                };
            }
            catch (Exception ex)
            {
                return new ApiPaging<QuotationOut>
                {
                    Code = ApiEnums.Fail,
                    Message = $"查询失败：{ex.Message}",
                    Total = 0,
                    Items = new List<QuotationOut>(),
                    PageIndex = input.PageIndex,
                    PageSize = input.PageSize
                };
            }
        }
        /// <summary>
        /// 获取报价详情
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public ApiResult<QuotationModel> QuotationModel(long Id)
        {
           var model = _quotationReposiotry.GetModel(Id);
            if (model == null)
            {
                return new ApiResult<QuotationModel>
                {
                    Code = ApiEnums.Fail,
                    Data = model,
                    Message = "查询失败"
                };
            }
            else
            {
                return new ApiResult<QuotationModel>
                {
                    Code = ApiEnums.Success,
                    Message = "查询成功",
                    Data = model
                };
            }
        }
    }
}
